/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.academik.controller;

import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.academik.model.Nivel;
import org.academik.model.Serie;
import org.academik.util.FacesUtil;
import org.academik.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

/**
 *
 * @author Javier
 */
@ManagedBean(name = "serieBean")
@ViewScoped
public class SerieBean extends AbstractViewScopedBean implements Serializable {

    /** Declaración de Variables*/
    private Serie serie;
    private ArrayList<Serie> series;
    private Serie selectedSerie;

    /** Creates a new instance of SerieBean */
    public SerieBean() {
        this.nuevo();
    }

    @Override
    public void crear() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = (Transaction) session.beginTransaction();
            session.saveOrUpdate(this.serie);
            session.flush();
            session.clear();
            tx.commit();
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO, "Información:", "La serie se creo satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesUtil.hibernateException(e);
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        Serie s = (Serie) session.load(Serie.class, this.selectedSerie.getIdSerie());
        try {
            tx = (Transaction) session.beginTransaction();
            session.delete(s);
            session.flush();
            session.clear();
            tx.commit();
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información:", "Registro eliminado satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesUtil.hibernateException(e);
        } finally {
            session.close();
        }
    }

    @Override
    public void cargarOpciones() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void cargarOpciones(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void buscar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modificar() {
        this.serie = this.selectedSerie;
    }

    @Override
    public void cargarResgistros() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void nuevo() {
        this.serie = new Serie();
        this.series = new ArrayList<Serie>();
        this.selectedSerie = new Serie();
        this.populateSeries();
    }

    /** Muestra los datos de la BD en la tabla*/
    public void populateSeries() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria cNivel = session.createCriteria(Serie.class);
        cNivel.addOrder(Order.asc("vcSerie"));
        this.series = (ArrayList<Serie>) cNivel.list();
        session.close();
    }

    /* Getters
     *   &
     * Setters
     */
    public Serie getSelectedSerie() {
        return selectedSerie;
    }

    public void setSelectedSerie(Serie selectedSerie) {
        this.selectedSerie = selectedSerie;
    }

    public Serie getSerie() {
        return serie;
    }

    public void setSerie(Serie serie) {
        this.serie = serie;
    }

    public ArrayList<Serie> getSeries() {
        return series;
    }

    public void setSeries(ArrayList<Serie> series) {
        this.series = series;
    }
}
